package com.example.controller;

import com.example.pojo.PrivateCourse;
import com.example.service.CoachService;
import com.example.service.PrivateCourseService;
import com.example.utils.FileUtil;
import com.jcraft.jsch.SftpException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/privateCourse")
public class PrivateCourseController {
    @Autowired
    private PrivateCourseService privateCourseService;

    @Autowired
    private CoachService coachService;

    @GetMapping("/getOneById")
    public PrivateCourse selectPrivateCourseById(int id){
        return privateCourseService.selectPrivateCourseById(id);
    }

    @GetMapping("/getAll")
    public List<PrivateCourse> selectAllPrivateCourse(){
        return privateCourseService.selectAllPrivateCourse();
    }

    @PostMapping("/insert")
    public int insertPrivateCourse(PrivateCourse privateCourse, MultipartFile file,int cid) throws SftpException, IOException {
        privateCourse.setCoach(coachService.selectCoachById(cid));
        privateCourse.setPicurl(FileUtil.upload(file));
        return privateCourseService.insertPrivateCourse(privateCourse);
    }

    @PostMapping("/delete")
    public int deletePrivateCourse(int id){
        return privateCourseService.deletePrivateCourseById(id);
    }

    @PostMapping("/update")
    public int updatePrivateCourse(PrivateCourse privateCourse, MultipartFile file) throws SftpException, IOException {
        privateCourse.setPicurl(FileUtil.upload(file));
        return privateCourseService.updatePrivateCourse(privateCourse);
    }
}
